package com.hk.core.authentication.api.validatecode.sms;

import com.hk.commons.sms.SmsSender;
import com.hk.commons.util.StringUtils;
import com.hk.core.authentication.api.validatecode.ValidateCodeStrategy;
import lombok.Setter;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author kevin
 * @date 2019-8-8 15:19
 */
public class SimpleParameterSmsCodeProcessor extends AbstractSmsValidateCodeProcessor {

    @Setter
    private String message = "短信验证码:${code}, 有效期: ${expire} 分钟";

    public SimpleParameterSmsCodeProcessor(SmsSender<?> smsSender,
                                           ValidateCodeStrategy<SmsValidateCode> validateCodeStrategy) {
        super(smsSender, validateCodeStrategy);
    }

    @Override
    protected void send(SmsValidateCode validateCode) throws IOException {
        Map<String, Object> param = HashMap.newHashMap(2);
        param.put("code", validateCode.getCode());
        param.put("expire", validateCode.getExpireSecond() / 60);
        smsSender.sendSms(validateCode.getPhone(), StringUtils.processTemplate(this.message, param));
    }
}
